package cn.sanli.manage.service.impl;

import cn.sanli.manage.ex.ServiceException;
import cn.sanli.manage.mapper.data1.CorresMapper;
import cn.sanli.manage.pojo.dto.System.CorresDTO;
import cn.sanli.manage.pojo.dto.System.UpdateCorresDTO;
import cn.sanli.manage.pojo.entity.Corres;
import cn.sanli.manage.pojo.vo.UserMessage;
import cn.sanli.manage.service.CorresService;
import cn.sanli.manage.utils.TreeUtils;
import cn.sanli.manage.web.ServiceCode;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字段映射表(corres)表服务实现类
 *
 * @author makejava
 * @since 2023-12-04 15:33:00
 */
@Service("corresService")
public class CorresServiceImpl implements CorresService {
    @Resource
    private CorresMapper corresMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Corres queryById(Integer id) {
        return this.corresMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增数据
     *
     * @param corres 实例对象
     * @return 实例对象
     */
    @Override
    public Corres insert(Corres corres) {
        this.corresMapper.insert(corres);
        return corres;
    }

    /**
     * 修改数据
     *
     * @param corres 实例对象
     * @return 实例对象
     */
    @Override
    public Corres update(Corres corres) {
        this.corresMapper.update(corres);
        return this.queryById(corres.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.corresMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 查询菜单
     *
     * @param message 登录用户信息
     * @return 菜单列表
     */
    @Override
    public List<Corres> findNodes(UserMessage message) {
        int roleId = message.getRoleId();
        List<Corres> treeList = null;
        if (roleId == 1 || roleId == 7) {
            //查看全部
            List<Corres> sysMenuList = corresMapper.selectAll();
            if (CollectionUtils.isEmpty(sysMenuList)) return null;
            treeList = TreeUtils.buildTree(sysMenuList); //构建树形数据
        } else if (roleId == 2 || roleId == 3) {
            //查看全部考核和奖励
            List<Corres> sysMenuList = corresMapper.selectAwardPunish();
            if (CollectionUtils.isEmpty(sysMenuList)) return null;
            treeList = TreeUtils.buildTree(sysMenuList); //构建树形数据
        } else if (roleId == 4) {
            //查看大口考核和奖励
            List<Corres> sysMenuList = corresMapper.selectCenter();
            if (CollectionUtils.isEmpty(sysMenuList)) return null;
            treeList = TreeUtils.buildTree(sysMenuList); //构建树形数据
        } else if (roleId == 5 || roleId == 6/* || roleId == 8*/) {
            //查看部门考核和奖励
            List<Corres> sysMenuList = corresMapper.selectDept();
            if (CollectionUtils.isEmpty(sysMenuList)) return null;
            treeList = TreeUtils.buildTree(sysMenuList); //构建树形数据
        }
        return treeList;
    }

    /**
     * 查询考核分级
     *
     * @param message 登录用户信息
     * @return
     */
    @Override
    public List<Corres> findAssess(UserMessage message) {
        int roleId = message.getRoleId();
        List<Corres> list = null;
        if (roleId == 1 || roleId == 2 || roleId == 7) {
            //查看全部
            list = corresMapper.selectAssess();
        } else if (roleId == 3 || roleId == 4) {
            //查看大口
            list = corresMapper.selectCenterAssess();
        } else if (roleId == 5 || roleId == 6 /*|| roleId == 8*/) {
            //查看部门
            list = corresMapper.selectDeptAssess();
        }
        return list;
    }

    /**
     * 查询奖励分级
     *
     * @param message
     * @return
     */
    @Override
    public List<Corres> findReward(UserMessage message) {
        int roleId = message.getRoleId();
        List<Corres> list = null;
        if (roleId == 1 || roleId == 2 || roleId == 7) {
            //查看全部
            list = corresMapper.selectReward();
        } else if (roleId == 3 || roleId == 4) {
            //查看大口
            list = corresMapper.selectCenterReward();
        } else if (roleId == 5 || roleId == 6 || roleId == 8) {
            //查看部门
            list = corresMapper.selectDeptReward();
        }
        return list;
    }

    /**
     * @return
     */
    @Override
    public List<Corres> findFaultLevel() {
        return corresMapper.selectFaultLevel();
    }

    @Override
    public List<Corres> findAssessCategory() {
        return corresMapper.selectAssessCategory();
    }

    /**
     * 查询处理形式
     *
     * @return
     */
    @Override
    public List<Corres> findAssessWay() {
        return corresMapper.selectAssessWay();
    }

    /**
     * 查询其他考核方式
     *
     * @return
     */
    @Override
    public List<Corres> findOtherAssessWay() {
        return corresMapper.selectOtherAssessWay();
    }

    /**
     * 查询其他荣誉处理
     *
     * @return
     */
    @Override
    public List<Corres> findOtherRewardWay() {
        return corresMapper.selectOtherRewardWay();
    }

    /**
     * 查询处理方式
     *
     * @return
     */
    @Override
    public List<Corres> findPunish() {
        return corresMapper.selectPunish();
    }

    /**
     * 添加菜单
     * @param corresDTO 添加信息
     */
    @Override
    public void saveMenu(CorresDTO corresDTO) {
        Corres corres = new Corres();
        corres.setName(corresDTO.getName());
        corres.setParentId(corresDTO.getParentId());
        corres.setValue(corresDTO.getValue());
        corresMapper.insertMenu(corres);
    }

    /**
     * 修改菜单
     * @param corresDTO 修改信息
     */
    @Override
    public void updateMenu(UpdateCorresDTO corresDTO) {
        Corres corres = new Corres();
        corres.setId(corresDTO.getId());
        corres.setName(corresDTO.getName());
        corres.setParentId(corresDTO.getParentId());
        corres.setValue(corresDTO.getValue());
        corresMapper.updateMenu(corres);
    }

    /**
     * 删除菜单
     * @param id 菜单id
     */
    @Override
    public void deleteMenu(Integer id) {
        int count = corresMapper.countByParentId(id);   // 先查询是否存在子菜单，如果存在不允许进行删除
        if (count > 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE, "该节点下有子节点，不可以删除");
        }
        // 不存在子菜单直接删除
        corresMapper.updateIsDeleteById(id);
    }

}

